using Gateway.Domain.Entities;
using Gateway.IApplication.DescribeDomain;
using Gateway.IApplication.DescribeDomain.Input;
using Gateway.IRepositories;
using Microsoft.EntityFrameworkCore;

namespace Gateway.Application;
/// <summary>
/// 
/// </summary>
public class DescribeDomainApp: IDescribeDomainApp
{
    private readonly IAppRepository<DescribeDomain> _repository;
    private readonly IUnitOfWork _unitOfWork;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="repository"></param>
    /// <param name="unitOfWork"></param>
    public DescribeDomainApp(IAppRepository<DescribeDomain> repository, IUnitOfWork unitOfWork)
    {
        _repository = repository;
        _unitOfWork = unitOfWork;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<bool> Save(CreateDescribeDomainInput input)
    {
        var entity = await _repository.Entities.Include(x => x.Records)
            .FirstOrDefaultAsync(p => p.DomainName.Equals(input.DomainName));
        if (entity != null)
        {
            if (input.Records?.Count > 0)
            {
                entity.SetRecord(input.Records.Select(p=>new DescribeDomainRecord()
                {
                    DomainId = entity.Id,
                    Status = p.Status,
                    Type = p.Type,
                    Remark = p.Remark,
                    TTL = p.TTL,
                    RecordId = p.RecordId,
                    RR = p.RR,
                    DomainName = p.DomainName,
                    Weight = p.Weight,
                    Value = p.Value,
                    Endpoint = p.Endpoint
                }).ToList());
            }
            else
            {
                entity.SetRecord(null);
            }
            entity.Update(input.DomainName, input.DomainId, input.AliDomain, input.InstanceEndTime, input.InstanceExpired, input.DnsServers, input.Endpoint);
            await _repository.UpdateAsync(entity);
        }
        else
        {
            var model = new DescribeDomain(input.DomainName, input.DomainId, input.AliDomain, input.InstanceEndTime, input.InstanceExpired, input.DnsServers, input.Endpoint);
            if (input.Records is { Count: > 0 })
            {
                model.SetRecord(input.Records.Select(p => new DescribeDomainRecord()
                {
                    DomainId = model.Id,
                    Status = p.Status,
                    Type = p.Type,
                    Remark = p.Remark,
                    TTL = p.TTL,
                    RecordId = p.RecordId,
                    RR = p.RR,
                    DomainName = p.DomainName,
                    Weight = p.Weight,
                    Value = p.Value,
                    Endpoint = p.Endpoint
                }).ToList());
            }
            else
            {
                model.SetRecord(null);
            }
           await _repository.AddAsync(model);
        }
        await _unitOfWork.CommitAsync();
        return true;
    }
}